package com.itheima.jdbc;

import com.itheima.utils.JDBCUtils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 用户登录小案例
 */
public class JDBCLogin {

    public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
        //获取连接对象
        Connection connection = JDBCUtils.getConnection();

        Scanner scanner = new Scanner(System.in);
        System.out.println("输入用户名：");
        String username = scanner.nextLine() ;
        System.out.println("输入密码：");
        String password = scanner.nextLine() ;

        //拼接 sql
        String sql = "select * from emp where username = '" + username + "' and password = '" + password + "'";

        //获取执行对象
        Statement statement = connection.createStatement();

        //执行
        ResultSet resultSet = statement.executeQuery(sql);

        if (resultSet.next()) {
            System.out.println("登陆成功");
        } else {
            System.out.println("登陆失败 用户名或密码错误");
        }
    }
}
